﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BaseHelper.Text;

namespace DeviceSDK.LED.ZhongHai.ZhongHai
{
    public class PlayBill
    {

        private Encoding _encoding = Encoding.Default;
        private List<object> _ltItem = new List<object>();
       
        //[LIST]
        //ItemCount=002
        //Item00=2,1,0,1,1,\C000000\Fs32\T255000000000\B000000000000\U深圳显科科技有限公司
        //Item01=2,1,0,1,1,\C000000\Fs32\T000255000000\B000000000000\I000

        public List<object> Items
        {
            get 
            {
                return _ltItem;
            }
            set
            {
                _ltItem = value;
            }
        }


        public void Clear()
        {
            _ltItem.Clear();
        }

        public void RemoveLastItem()
        {
            if (_ltItem.Count > 0)
                _ltItem.RemoveAt(_ltItem.Count - 1);
        }

  

        public void AddString(string Text, int PlayTime = 10, string FontName = "黑体", int FontSize = 0, string FontColor = "#00FF00",int Width = 0, int Height = 0)
        {
            StringItem item = new StringItem();
            //1B 36 01 
            //01 2C 
            //1B 35 20 1B 34 00 1B 31 03 
            //C4 FE 41 31 32 33 34 35 D2 D1 B3 AC CB D9 D0 D0 CA BB A3 A1
            Text = Text.Replace("\r\n", "\n");
            PlayTime *= 100;

            item.FontName = FontNameFormat(FontName);
            item.FontColor = ColorFormat(FontColor);
            item.EffectIn = new byte[]{0x1B,0x36,0x01};
            item.PlayTime = new byte[]{(byte)(PlayTime>>8),(byte)PlayTime};


            //判断是否是F架，因为F架一般屏幕比1:2，长是宽2倍，这里判断小于比例3就是F架
            bool IsFMode = ((float)Width / (float)Height) <= 3.0;


            //如果是F架的屏，不进行折行行计算，所有内容直接显示一屏。如果要多行请AddString多行
            //则不计算居中的X,Y，直接输出显示。
            if (IsFMode)
            {
                if (FontSize <= 1)
                {
                    FontSize = TextFontSizeHelper.CalcFontSizeByM(Text,  Width,Height);
                }

                item.FontSize = FontSizeFormat(FontSize);
                item.Text = Encoding.Default.GetBytes(Text);
                item.Position = new byte[] { 0x1b, 0x32, 0x00, 0x00, 0x00, 0x00 };  //设置默认X,Y为0，0坐标
                _ltItem.Add(item);
            }
            else
            {
                //如果是龙门架的屏

                //如果包含回车，则自动分页
                if (Text.Contains("\n"))
                {
                    string[] Texts = Text.Split('\n');
                    foreach (string T in Texts)
                    {
                        if (T.Length == 0)
                            continue;

                        StringItem stmp = (StringItem)item.Clone();
                        int tmpFontSize = FontSize;
                        if (FontSize <= 1)
                        {
                            tmpFontSize = TextFontSizeHelper.CalcFontSizeByM(T,  Width,Height);
                        }

                        stmp.Text = Encoding.Default.GetBytes(T);
                        stmp.FontSize = FontSizeFormat(tmpFontSize);
                        stmp.Position = TextFormat(stmp.FontSize[2], Width, Height, T);
                        _ltItem.Add(stmp);
                    }
                }
                else
                {
                    if (FontSize <= 1)
                    {
                        FontSize = TextFontSizeHelper.CalcFontSizeByM(Text, Width,Height);
                    }

                    item.FontSize = FontSizeFormat(FontSize);
                    item.Text = Encoding.Default.GetBytes(Text);
                    item.Position = TextFormat(item.FontSize[2], Width, Height, Text);
                    _ltItem.Add(item);
                }
            }
        }


        private byte[] TextFormat(int FontSize,int Width,int Height,string Text)
        {

            int Remain = 0;

            try
            {
                foreach (char c in Text)
                {
                    if ((int)c <= 127)
                        Remain += FontSize / 2;
                    else
                        Remain += FontSize;
                }
                int x = (Width - Remain) / 2;
                int y = (Height - FontSize) / 2;
                return new byte[] { 0x1B, 0x32, 0x00, (byte)x, 0x00, (byte)y };
            }
            catch { }

            return new byte[] { 0x1b,0x32,0x00,0x00,0x00,0x00};
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ColorHex">#FF0000红，#FFFF00黄，#00FF00绿</param>
        /// <returns></returns>
        private byte[] ColorFormat(string ColorHex)
        { 
            switch(ColorHex)
            {
                case "#00FF00": return new byte[]{0x1B ,0x31,0x01};//绿
                case "#FF0000": return new byte[]{0x1B ,0x31,0x00};//红
                case "#FFFF00": return new byte[]{0x1B ,0x31,0x03};//黄
                                                             //0x02是蓝色
                                                             //0x04是白色
            }
            return new byte[]{0x1B ,0x31,0x00};
        }

        private byte[] FontNameFormat(string Font)
        {
            switch (Font)
            {
                case "宋体": return new byte[]{0x1B,0x34,0x02};
                case "仿宋": return new byte[]{0x1B,0x34,0x03};
                case "楷体": return new byte[]{0x1B,0x34,0x01} ;
                case "黑体": return new byte[]{0x1B,0x34,0x00 };
            }
            return new byte[]{0x1B,0x34,0x00 };
        }

        private byte[] FontSizeFormat(int FontSize)
        {
  
            int[] fontSize = new int[] { 64,48,32,24,16 };
            for (int i = 0; i < fontSize.Length; i++)
            {
                if (FontSize >= fontSize[i])
                    return new byte[]{0x1B,0x35,(byte)fontSize[i]};
            }

            return new byte[] { 0x1B, 0x35, (byte)32 };
        }

        public byte[] Create()
        {
            //立即显示，红色，黑体，32点阵，“宁A12345已超速行驶！”
            //if (File.Exists(FilePath))
            //    File.Delete(FilePath);
            //1B 36 01 01 2C
            //1B 30 换行
            //1B 37 02 字符间距2
            //1B 32 00 03 00 03  显示坐标
            byte[] output=new byte[]{};
            //效果1内容1效果2内容2
            for (int i = 0; i < _ltItem.Count; i++)
            {
                if (_ltItem[i] is StringItem)
                {
                    StringItem item = (StringItem)_ltItem[i];
                    byte[] buffer=new byte[item.PlayTime.Length+18+item.Text.Length];

                    Buffer.BlockCopy(item.EffectIn, 0, buffer, 0, 3);
                    Buffer.BlockCopy(item.PlayTime, 0, buffer, 3, item.PlayTime.Length);
                    Buffer.BlockCopy(item.FontColor, 0, buffer, item.PlayTime.Length + 3, 3);
                    Buffer.BlockCopy(item.FontName, 0, buffer, item.PlayTime.Length + 6, 3);
                    Buffer.BlockCopy(item.FontSize, 0, buffer, item.PlayTime.Length + 9, 3);
                    Buffer.BlockCopy(item.Position, 0, buffer, item.PlayTime.Length + 12, 6);
                    Buffer.BlockCopy(item.Text, 0, buffer, item.PlayTime.Length + 18, item.Text.Length);
                    
                    byte[] tmp=new byte[output.Length+buffer.Length];
                    Buffer.BlockCopy(output, 0, tmp, 0, output.Length);
                    Buffer.BlockCopy(buffer,0,tmp,output.Length,buffer.Length);
                    output = tmp;
                }
            }

            int count = output.Length + 2;
            if (count % 2 == 1)
                count--;
            count /= 2;

            byte[] totalBuffer=new byte[4+output.Length];
            totalBuffer[0] = 0;
            totalBuffer[1] = (byte)count;
            totalBuffer[2] = (byte)(count * 2);
            totalBuffer[3] = (byte)count;

            Buffer.BlockCopy(output, 0, totalBuffer, 4, output.Length);

            return totalBuffer;

        }

    }

    class ItemBase
    {
        public byte[] Position = new byte[] { 0x1B, 0x32, 0x00,0x00,0x00, 0x00 };//坐标

        public byte[] PlayTime = new byte[]{0x01,0xFF};    //0.1s 默认      
        public byte[] EffectIn = new byte[]{0x1B,0x36,0x01};

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }


    
    class StringItem : ItemBase
    {
        public byte[] FontName;
        public byte[] FontSize;
        public byte[] FontColor;
        public byte[] Text;  
    }
    
}
